package com.woniuxy.hrp.web;

import com.woniuxy.hrp.Vo.PatientPswUpdateVo;
import com.woniuxy.hrp.entity.*;
import com.woniuxy.hrp.security.JwtUtil;
import com.woniuxy.hrp.service.PayRecordService;
import com.woniuxy.hrp.service.WalletService;
import com.woniuxy.hrp.service.WalletoprService;
import com.woniuxy.hrp.unityhandler.ResponseEntity;
import com.woniuxy.hrp.service.PatientService;
import com.woniuxy.hrp.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description：11
 * @Author：曹杰克
 * @CreateTime：2022/8/30 9:38
 */
@CrossOrigin
@Transactional
@RestController
@RequestMapping("/api/v1/patient")
@Slf4j
public class PatientController {
    @Autowired
    PatientService patientService;
    @Autowired
    WalletService walletService;

    @Autowired
    private WalletoprService walletoprService;

    @Autowired
    private PayRecordService payRecordService;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 根据用户id获取用户信息
     * @param
     * @return
     */
    @RequestMapping("/getPatient")
    public ResponseEntity getPatientById(@RequestHeader String jwt){ //获取jwt中的id信息
        int id = (int)jwtUtil.getInfo(jwt, "id");
        Patient patient = patientService.getById(id);
        return new ResponseEntity(patient);
    }

    @RequestMapping("/updatePatient")
    public ResponseEntity updatePatientByCondition(@RequestBody Patient patient){
        patientService.updateByCondition(patient);
        return ResponseEntity.SUCCESS;
    }
    @RequestMapping("/updatePassWord")
    public ResponseEntity updatePatientPassWord(@RequestBody PatientPswUpdateVo patientPswUpdateVo){
        System.out.println(patientPswUpdateVo);
        int id =patientPswUpdateVo.getId();
        String oldPsw = patientPswUpdateVo.getOldPassword();
        String newPsw = patientPswUpdateVo.getNewPassword();
        String confirm = patientPswUpdateVo.getConfirm();
        if (oldPsw == null || newPsw == null || confirm == null){
            return new ResponseEntity("701","empty");  //数据空
        }
        if (!confirm.equals(newPsw)){
            return new ResponseEntity("702","confirmError"); //确认密码与新密码不一致
        }
        Patient patient = patientService.getById(id);
        if (!patient.getPassword().equals(oldPsw)){  //原密码输入错误
            return new ResponseEntity("405","oldError");
        } else {
            patient.setPassword(newPsw);
            patientService.updatePassWord(patient);
            return ResponseEntity.SUCCESS;
        }

    }



    /**
     * 查询预约信息（按条件查询） 连预约信息表
     * 预约信息表中状态：T-待完成的预约 D-已完成预约 U-未付款的预约
     *
     * @param status
     * @author shanshanfan
     * @return
     */
    @RequestMapping("/selectbyrestatus")
    public ResponseEntity<List<Reservation>> updatePatientPassWord(String status){
        patientService.getReservationByStatus(status);
        return ResponseEntity.SUCCESS;
    }

    /**
     * 刘磊
     * @param patientFee
     * @return
     * 支付费用
     */
    @RequestMapping("/paybywallet")
    public ResponseEntity paybywallet(@RequestBody  PatientFee patientFee){
        Wallet byPatientId = walletService.getByPatientId(patientFee.getId());
        BigDecimal balance = byPatientId.getBalance();
        if(patientFee.getFee().compareTo(balance)>0){
            //跳转到支付宝支付
            return ResponseEntity.FAIL;
        }else{
            byPatientId.setBalance(patientFee.getFee().negate());
            walletService.updateWallet(byPatientId);
            return ResponseEntity.SUCCESS;
        }
    }

    /***
     * @description: 多条件分頁查询指定患者的钱包操作记录
     * @param: 患者id，查询条件
     * @return: com.woniuxy.hrp.unityhandler.ResponseEntity
     * @author Lrw
     * @date: 2022/9/1 1:40
     */
    @PostMapping("/getWalletoprsByCondition/{patientId}/{pageSize}/{page}")
    public ResponseEntity getWalletOprsByCondition(@PathVariable("patientId") int patientId,
                                                                @PathVariable("pageSize") int pageSize,
                                                               @PathVariable("page") int page,
                                                               @RequestBody Walletopr walletopr){

        PageBean<Walletopr> pageBean = walletoprService.getWalletoprsByPatientIdAndConditionAndPage(patientId, pageSize, page, walletopr);
        return new ResponseEntity("200","ok",pageBean);
    }

    /***
     * @description: 多条件查询指定患者的交易记录
     * @param: 患者id、分页大小、当前页、查询条件
     * @return: com.woniuxy.hrp.unityhandler.ResponseEntity
     * @author Lrw
     * @date: 2022/9/7 16:37
     */
    @PostMapping("/getPayrecordsByCondition/{patientId}/{pageSize}/{page}")
    public ResponseEntity getPayrecordsByCondition(@PathVariable("patientId") int patientId,
                                                   @PathVariable("pageSize") int pageSize,
                                                   @PathVariable("page") int page,
                                                   @RequestBody PayRecord payRecord){
        PageBean<PayRecord> pageBean = payRecordService.getPayRecordsByPatientIdAndConditionAndPage(patientId, pageSize, page, payRecord);
        return new ResponseEntity("200","ok",pageBean);
//        return null;
    }


    /***
     * @description: 分页查询患者的所有支付记录
     * @param: patient
     * @return: com.woniuxy.hrp.unityhandler.ResponseEntity
     * @author Lrw
     * @date: 2022/9/1 1:39
     */
    @GetMapping("/getPayRecords/{patientId}/{pageSize}/{page}")
    public ResponseEntity getPayRecords(@PathVariable("patientId") int patientId,
                                        @PathVariable("pageSize") int pageSize,
                                        @PathVariable("page") int page){
        PageBean<PayRecord> pageBean = payRecordService.getAllByPatientIdAndPage(patientId, page);
        return new ResponseEntity("200","ok",pageBean);
    }

    /***
     * @description: 获取指定患者的支付总笔数
     * @param: patient
     * @return: com.woniuxy.hrp.unityhandler.ResponseEntity
     * @author Lrw
     * @date: 2022/9/1 2:13
     */
    @GetMapping("/getCostNum/{patientId}")
    public ResponseEntity getCostNum(@PathVariable int patientId){
        List<PayRecord> payRecords = payRecordService.getAllByPatientId(patientId);
        return new ResponseEntity(payRecords.size());
    }

    /***
     * @description: 获取指定患者的支付总额
     * @param: patient
     * @return: com.woniuxy.hrp.unityhandler.ResponseEntity
     * @author Lrw
     * @date: 2022/9/1 2:18
     */
    @GetMapping("/getTotalCost/{patientId}")
    public ResponseEntity getTotalCost(@PathVariable int patientId){
        List<PayRecord> payRecords = payRecordService.getAllByPatientId(patientId);
        BigDecimal total = new BigDecimal("0");
        for (int i = 0; i < payRecords.size(); i++) {
            total=payRecords.get(i).getFee().add(total);
        }
        return new ResponseEntity(total);
    }

    /***
     * @description: 获取患者id
     * @param: jwt
     * @return: com.woniuxy.hrp.unityhandler.ResponseEntity
     * @author Lrw
     * @date: 2022/9/9 11:53
     */
    @GetMapping("/getId")
    public ResponseEntity getPatientId(@RequestHeader String jwt){
        int id=(int)jwtUtil.getInfo(jwt,"id");
        return new ResponseEntity(id);
    }
}
